// pages/direct/direct.js
var app = getApp()
Page({
  data: {
    hiddens: true,
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    current: 1,
    circular: true
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        })
      }
    })
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    getBackData(this)//获取普通商品
    getBackDataHot(this)//获取爆款
    this.setData({
      zyNum: wx.getStorageSync('directCarts').length
    })
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭

  },
  switchnav: function (e) {
    this.setData({
      current: e.target.dataset.current
    })
    if (e.target.dataset.current == 1 && !this.data.currentPage1) {
      getBackData(this, 1)
    } else if (e.target.dataset.current == 2 && !this.data.currentPage2) {
      getBackData(this, 2)
    } else if (e.target.dataset.current == 3 && !this.data.currentPage3) {
      getBackData(this, 3)
    }
  },
  getSearch: function (e) {//获取搜索框的值
    if (e.detail.value != "") {
      this.setData({
        searchs: e.detail.value
      })
    }
  },

  searchDirect: function () {//获取搜索商品
    var that = this;
    if (!this.data.searchs) {
      return
    }
    wx.request({
      url: app.globalData.allUrl + "/YMGW/directAction/getDirectProduct",
      data: {
        pageNum: 1,
        pageCount: 100,
        productName: that.data.searchs
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        //console.log(res.data)
        if (res.data) {
          that.setData({
            DirectProduct: res.data.list,
            currentPage1: false,
            currentPage2: false,
            currentPage3: false,
              current:-1,
          })

        }
      }
    })
  },

  toDetail: function (e) {//跳转到详情页面
    wx.navigateTo({
      url: '/pages/directDetail/directDetail?id=' + e.target.dataset.id,
    })
  }
})


function getBackData(that, classifys) {//从后台获取直营普通商品
  if (!classifys) {
    classifys = that.data.current
    that.setData({//储存当前页已经加载过的信息
      currentPage1: true
    })
  } else {
    if (classifys == 1) {
      that.setData({//储存当前页已经加载过的信息
        currentPage1: true
      })
    } else if (classifys == 2) {
      that.setData({//储存当前页已经加载过的信息
        currentPage2: true
      })
    } else if (classifys == 3) {
      that.setData({//储存当前页已经加载过的信息
        currentPage3: true
      })
    }
  }
 // console.log(classifys)
  wx.request({
    url: app.globalData.allUrl + "/YMGW/directAction/getDirectProduct",
    data: {
      pageNum: 1,
      pageCount: 100,
      classify: classifys,
      putaway: 1,

      // type:0//加载普通商品0，由于前期商品少，故注释
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      // console.log(res.data)
      if (res.data &&res.data.list.length > 0) {
        if (classifys==1){
          that.setData({
            DirectProduct1: res.data.list,
          })
        } else if (classifys == 2){
          that.setData({
            DirectProduct2: res.data.list,
          })
        } else if (classifys == 3) {
          that.setData({
            DirectProduct3: res.data.list
          })
        }
      }
    }
  })
}

function getBackDataHot(that) {//从后台获取直营商品的爆款商品
  wx.request({
    url: app.globalData.allUrl + "/YMGW/directAction/getDirectProduct",
    data: {
      pageNum: 1,
      pageCount: 100,
      putaway: 1,
      type: 1
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
     // console.log(res.data)
      if (res.data &&res.data.list.length > 0) {
        var imgUrls = [];
        for (var i = 0; i < res.data.list.length; i++) {
          imgUrls.push({
            link: '/pages/directDetail/directDetail?id=' + res.data.list[i].id,
            url: 'https://www.ymg3.com/ymgImage/directs/' + res.data.list[i].adImg
          })
        }
        that.setData({
          imgUrls: imgUrls
        })
      }
    }
  })
}